package com.sino.operation.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sino.operation.commons.response.CenterResponseEntity;
import com.sino.operation.entity.SysCenter;
import com.sino.operation.service.SysCenterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("centerManager")
@Api(value = "中心管理",tags="中心管理")
public class CenterManagerController {
    @Autowired
    private SysCenterService sysCenterService;

    @GetMapping("/selectCenterList")
    @ApiOperation(value = "查询中心列表")
    @ResponseBody
    public  IPage<SysCenter>  getCenterList(@RequestParam Map<String,String> map){
        IPage<SysCenter> sysCenterIPage = sysCenterService.selectList(map);
        return sysCenterIPage;
    }

    @PostMapping("/addCenter")
    @ApiOperation(value = "新增中心数据")
    @ResponseBody
    public CenterResponseEntity addCenter(SysCenter sysCenter){
        try {
            sysCenterService.saveOrUpdate(sysCenter);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CenterResponseEntity.build();
    }

    @PostMapping("/deleteCenter")
    @ApiOperation(value = "批量删除中心数据")
    @ResponseBody
    public CenterResponseEntity deleteCenter(List ids){
        try {
            sysCenterService.removeByIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return CenterResponseEntity.buildError();
        }
        return CenterResponseEntity.build();
    }

    @GetMapping("/selectCenterById")
    @ApiOperation(value = "根据中心号查询中心信息")
    @ResponseBody
    public CenterResponseEntity selectCenterById(@RequestParam String centerNo){
        SysCenter sysCenter=null;
        try {
             sysCenter = sysCenterService.getById(centerNo);
        } catch (Exception e) {
            e.printStackTrace();
            return CenterResponseEntity.buildError();
        }
        return CenterResponseEntity.build(SysCenter.class).setData(sysCenter);
    }
    @PostMapping("/updateCenter")
    @ApiOperation(value = "修改中心信息")
    @ResponseBody
    public CenterResponseEntity updateCenter(SysCenter sysCenter){
        try {
            sysCenterService.saveOrUpdate(sysCenter);
        } catch (Exception e) {
            e.printStackTrace();
            return CenterResponseEntity.buildError();
        }
        return CenterResponseEntity.build();
    }
    @PostMapping("/updateCenterById")
    @ApiOperation(value = "修改中心状态")
    @ResponseBody
    public CenterResponseEntity updateCenterById(@ApiParam("中心号")String centerNo, @ApiParam("修改中心状态(1-开启中心，2-关闭中心)")String flag){
        try {
            SysCenter Center = sysCenterService.getById(centerNo);
            if (flag.equals("1")){
                Center.setCenterState("1");
            }else {
                Center.setCenterState("2");
            }
            sysCenterService.saveOrUpdate(Center);
        } catch (Exception e) {
            e.printStackTrace();
            return CenterResponseEntity.buildError();
        }
        return CenterResponseEntity.build();
    }
}
